package com.panshi.util;

import com.panshi.entity.AppMatterT;
import com.panshi.service.AnswerClientService;
import com.panshi.service.QaClientService;
import com.panshi.service.UserClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class QaUtils {

    @Autowired
    private QaClientService qaClientService;

    //回答
    @Autowired
    private AnswerClientService answerClientService;

    @Autowired
    private UserClientService userClientService;

    //咨询分类查询
    public List<AppMatterT> qaList(int cid,int page, int limit, String type){
        System.out.println("分页查询问答-----------------》"+cid+"  "+page+"  "+limit+"  "+type);
        //问答列表
        List<AppMatterT> qaAppMatterTList = qaClientService.qaPage(cid,page, limit, type);
        if(qaAppMatterTList!=null){
            qaAppMatterTList.forEach(matterT->{
                //获取问答分类
                matterT.setClassifyT(qaClientService.bycid(matterT.getClassifyId()));
                //获取答题对象
                matterT.setAnswerT(answerClientService.bymid(matterT.getMatterId()));
                //获取用户对象
                matterT.setAppUserT(userClientService.byid(matterT.getUserId()));
                //回答用户对象
                if(matterT.getAnswerT()!=null){
                    matterT.setReplyUser(userClientService.byid(matterT.getAnswerT().getUserId()));
                }
            });
        }
        return qaAppMatterTList;
    }

    public List<AppMatterT> qaClickType(int cid ,int page,int limit,String type){
        List<AppMatterT> qaAppMatterTList = new ArrayList<>();

        //咨询
        if(type.equals("10")){  //推荐
            qaAppMatterTList = qaList(cid, page, limit, "read_count");
            Collections.shuffle(qaAppMatterTList);
        }else if(type.equals("15")){ //热度
            qaAppMatterTList = qaTypeList(cid, page, limit, "answer_count",1);
        }else if(type.equals("20")){//最新
            qaAppMatterTList = qaTypeList(cid, page, limit, "read_count",0);
        }

        if(qaAppMatterTList!=null && !qaAppMatterTList.isEmpty()){
            qaAppMatterTList.forEach(matterT->{
                //获取问答分类
                matterT.setClassifyT(qaClientService.bycid(matterT.getClassifyId()));
                //获取答题对象
                matterT.setAnswerT(answerClientService.bymid(matterT.getMatterId()));
                //获取用户对象
                matterT.setAppUserT(userClientService.byid(matterT.getUserId()));
                //回答用户对象
                if(matterT.getAnswerT()!=null){
                    matterT.setReplyUser(userClientService.byid(matterT.getAnswerT().getUserId()));
                }
            });
        }
        return qaAppMatterTList;
    }

    public String joinData(List<AppMatterT> matterTS){
        String info = "";
        for (int i=0;i<matterTS.size();i++){
            AppMatterT matter = matterTS.get(i);
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
            String format = sdf.format(matter.getGroundTime());

            info+="<div class=\"list-item\"\">\n" +
                    "<!-- 问题配图 -->\n" +
                    "<div class=\"list-item-top\">\n" +
                    "\t<a target=\"_blank\" class=\"type\" href=\"/answers_main/"+matter.getClassifyId()+"\" >"+matter.getClassifyT().getClassifyName()+"</a>\n" +
                    "\t<a target=\"_blank\" class=\"ansewer type\"\n" +
                    "\t   href=\"/answers_ptl/"+matter.getMatterId()+"\" >"+(matter.getAnswerCount()>0?"已回答":"未答")+"</a>\n" +
                    "\t<a target=\"_blank\" class=\"place\"\n" +
                    "\t   href=\"/answers_ptl/"+matter.getMatterId()+"\">邀请您回答</a>\n" +
                    "</div>\n" +
                    "<div class=\"list-item-title\">\n" +
                    "\t<a target=\"_blank\" href=\"/answers_ptl/"+matter.getMatterId()+"\" >"+matter.getMatterTitle()+"</a>\n" +
                    "</div>";
            if(matter.getAnswerCount()>0){
                info+="<div class=\"list-item-content-text\">\n" +
                        "\t<div class=\"text-ansower-user\">\n" +
                        "\t\t<a target=\"_blank\" href=\"/answers_ptl/"+matter.getMatterId()+"\"\n" +
                        "\t\t   class=\"user-img-name\">\n" +
                        "\t\t\t<img src=\""+matter.getReplyUser().getImgPath()+"\">\n" +
                        "\t\t\t<span class=\"user-name\">"+matter.getReplyUser().getUserNickname()+"</span>\n" +
                        "\t\t</a>\n" +
                        "\t\t<p class=\"text-ansower-p\">\n" +
                        "\t\t\t<span class=\"text-ansower-huida\">[回答内容]</span>\n" +
                        "\t\t</p>\n" +
                        "\t</div>\n" +
                        "\t<p class=\"text-ansower-content\">\n" +
                        "\t\t<a target=\"_blank\" href=\"/answers_ptl/"+matter.getMatterId()+"\">\n" +
                        "\t\t"+matter.getAnswerT().getAnswerContent().substring(0, 20)+"..."+"\n" +
                        "\t\t</a>\n" +
                        "\t</p>\n" +
                        "</div>";
            }
            info+="<div class=\"list-item-info\">\n" +
                    "\t<div class=\"list-item-info-left\">\n" +
                    "\t\t<a target=\"_blank\" href=\"/answers_ptl/"+matter.getMatterId()+"\"\n" +
                    "\t\t   class=\"user-img-name\">\n" +
                    "\t\t\t<img src=\""+matter.getAppUserT().getImgPath()+"\">\n" +
                    "\t\t\t<span class=\"user-name\">"+matter.getAppUserT().getUserNickname()+"</span>\n" +
                    "\t\t</a>\n" +
                    "\t\t<span class=\"time\"><span>"+format+"</span></span>\n" +
                    "\t\t<a target=\"_blank\" class=\"huida\"\n" +
                    "\t\t   href=\"/answers_ptl/"+matter.getMatterId()+"\"><span>"+matter.getAnswerCount()+"</span>个回答</a>\n" +
                    "\t\t<span class=\"dotted\"></span>\n" +
                    "\t\t<a target=\"_blank\" class=\"liulan\"\n" +
                    "\t\t   href=\"/answers_ptl/"+matter.getMatterId()+"\"><span>"+matter.getReadCount()+"</span>阅读</a>\n" +
                    "\t</div>\n" +
                    "\t<div class=\"list-item-info-right\">\n" +
                    "\t\t<a target=\"_blank\" href=\"/answers_ptl/"+matter.getMatterId()+"\">\n" +
                    "\t\t\t<img src=\"/icon/Write.png\" alt=\"有贝口腔网\">\n" +
                    "\t\t\t<span>我要回答</span>\n" +
                    "\t\t</a>\n" +
                    "\t</div>\n" +
                    "</div>";

        }
        return info;
    }


    //咨询分类查询
    public List<AppMatterT> qaTypeList(int cid,int page, int limit, String type,int answerCount){
        //问答列表
        List<AppMatterT> qaAppMatterTList = qaClientService.qaAnTypePage(cid,page, limit, type,answerCount);
        if(qaAppMatterTList!=null){
            qaAppMatterTList.forEach(matterT->{
                //获取问答分类
                matterT.setClassifyT(qaClientService.bycid(matterT.getClassifyId()));
                //获取答题对象
                matterT.setAnswerT(answerClientService.bymid(matterT.getMatterId()));
                //获取用户对象
                matterT.setAppUserT(userClientService.byid(matterT.getUserId()));
                //回答用户对象
                if(matterT.getAnswerT()!=null){
                    matterT.setReplyUser(userClientService.byid(matterT.getAnswerT().getUserId()));
                }
            });
        }
        return qaAppMatterTList;
    }

}
